package de.tabuma.wsdl2xform.history.view;

import de.tabuma.wsdl2xform.history.entity.JournalEntry;
import de.tabuma.lib.tabumaview;
import java.io.*;
import java.text.*;
import java.util.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;


/**
 *
 * @author $Author: alefen $
 * @version $Revision: 1.2 $
 * @since $Date: 2009/12/30 02:54:50 $
 */
public class view extends tabumaview {

    public view(String name, String strUrl, Hashtable<Integer, String> nameList, List<Integer> adminList) {
        super("",new Hashtable());
        this.name = name;
        this.strUrl = strUrl;
        this.nameList = nameList;
        this.adminList = adminList;
    }

    private String name = "";
    private String strUrl = "";
    private Hashtable<Integer,String> nameList;
    private List<Integer> adminList;

    private static Logger log = Logger.getLogger(view.class.getName());

    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out)  {
        try {

            UserService userService = UserServiceFactory.getUserService();

            if(request.getAttribute(de.tabuma.wsdl2xform.controller.constants.HISTORY)!=null) {

                DateFormat df = DateFormat.getDateTimeInstance();
                NumberFormat nf = NumberFormat.getNumberInstance();
                out.println("<div class=\"contentHeader\"><a href=\""+strUrl+"\">"+name+" - </a></div>");
                out.println("<div class=\"contentContent\">");
                out.println("<div class=\"historyHeader1\">"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.historyinformation")+"</div>");
                out.println("<div class=\"historyHeader2\">"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.changeparameter")+"</div>");
                List<JournalEntry> myList = (List<JournalEntry>) request.getAttribute(de.tabuma.wsdl2xform.controller.constants.HISTORY_LIST);
                if(myList!=null || myList.size()>0) {
                    Iterator<JournalEntry> myIt = myList.iterator();
                    while(myIt.hasNext()) {
                        JournalEntry je = myIt.next();
                        if(!adminList.contains(je.getParameterId()) || userService.isUserAdmin()) {
                            out.println("<div class=\"historyContent1\">");
                            if(userService.isUserAdmin()) {
                                out.println(je.getUser()+" "+df.format(je.getDate()));
                            }
                            else {
                                out.println(df.format(je.getDate()));
                            }
                            out.println("</div>");

                            out.println("<div class=\"historyContent2\">");
                            switch(je.getAction()) {

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_CANCEL:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_cancel"));
                                    break;

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_CREATE:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_create"));
                                    break;

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_REMOVE:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_remove"));
                                    break;

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_UPDATE:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_update"));
                                    break;

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_ELEMENT_ADD:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_element_add"));
                                    break;

                                case de.tabuma.wsdl2xform.history.logik.JournalEntryFacade.ACTION_ELEMENT_REMOVE:
                                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.action_element_remove"));
                                    break;

                            }

                            String journalEntryName = nameList.get(je.getParameterId());

                            switch(je.getParameterStyle()) {
                                case de.tabuma.wsdl2xform.history.entity.JournalEntry.PARAMETER_STYLE_DATE:
                                    if(je.getParameterDate()!=null) {
                                        out.println(journalEntryName+":"+df.format(je.getParameterDate()));
                                    }
                                    else {
                                        out.println(journalEntryName+":"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                                    }
                                    break;
                                case de.tabuma.wsdl2xform.history.entity.JournalEntry.PARAMETER_STYLE_DOUBLE:
                                    if(je.getParameterDouble()!=null) {
                                        out.println(journalEntryName+":"+nf.format(je.getParameterDouble()));
                                    }
                                    else {
                                        out.println(journalEntryName+":"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                                    }
                                    break;
                                case de.tabuma.wsdl2xform.history.entity.JournalEntry.PARAMETER_STYLE_LIST:
                                    if(je.getParameterList()!=null) {
                                        out.println(journalEntryName+":"+je.getParameterList());
                                    }
                                    else {
                                        out.println(journalEntryName+":"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                                    }
                                    break;
                                case de.tabuma.wsdl2xform.history.entity.JournalEntry.PARAMETER_STYLE_LONG:
                                    if(je.getParameterLong()!=null) {
                                        out.println(journalEntryName+":"+nf.format(je.getParameterLong()));
                                    }
                                    else {
                                        out.println(journalEntryName+":"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                                    }
                                    break;
                                case de.tabuma.wsdl2xform.history.entity.JournalEntry.PARAMETER_STYLE_STRING:
                                    if(true) {
                                        out.println(journalEntryName+":"+je.getParameterString());
                                    }
                                    else {
                                        out.println(journalEntryName+":"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                                    }
                                    break;
                            }


                            out.println("</div>");

                        }
                    }
                }
                else {
                    out.println("<div class=\"historyContent1\">");
                    out.println(java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("history.empty"));
                    out.println("</div>");
                }

                out.println("</div>");
            }
            else {
                strUrl += "&amp;";
                strUrl += de.tabuma.wsdl2xform.controller.constants.HISTORY;
                strUrl += "=";
                strUrl += "true";
                out.println("<div class=\"contentHeader\"><a href=\""+strUrl+"\">"+java.util.ResourceBundle.getBundle(de.tabuma.wsdl2xform.constants.projectLanguageBundle).getString("orders.details.history")+"+ </a></div>");
            }
        }
        catch(Exception e) {
            log.warning(e.getMessage());
        }
    }



}
